package com.cj.image.service;

import com.cj.image.bean.ImageTagBean;
import com.cj.image.bean.TagInfo;
import com.cj.image.mapper.TagMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TagService {
    @Autowired
    private TagMapper tagMapper;

    public void addTagInfo(TagInfo tagInfo) {
        List<TagInfo> tagInfos = tagMapper.getTagInfoByLevel(tagInfo.getTagLevel());
        List<TagInfo> collect = tagInfos.stream().sorted(new Comparator<TagInfo>() {
            @Override
            public int compare(TagInfo o1, TagInfo o2) {
                return Integer.parseInt(o2.getTagId()) - Integer.parseInt(o1.getTagId());
            }
        }).collect(Collectors.toList());

//        String tagIdMax = tagInfoMax.getTagId();
        String parentTagId = tagInfo.getParentTagId();
        if (tagInfo.getTagLevel().equals("1")) {
            String tagIdMax = collect.get(0).getTagId();
            int i = Integer.parseInt(tagIdMax.substring(0, 2));
            int i1 = i + 1;
            String changeI;
            if (i1 <= 9) {
                changeI = "0" + i1;
            } else {
                changeI = String.valueOf(i1);
            }
            if (i >= 99) {
                throw new RuntimeException("标签编号超过99不能添加啦");
            } else {
                String tagId = changeI + tagIdMax.substring(2, 6);
                tagInfo.setTagId(tagId);
            }
        } else {
            List<TagInfo> collect1 = collect.stream().filter(tagInfo1 -> tagInfo1.getParentTagId().equals(parentTagId)).collect(Collectors.toList());
            if (tagInfo.getTagLevel().equals("2")) {
                if (collect1.isEmpty()) {
                    String tagId = parentTagId.substring(0, 2)  + "0100";
                    tagInfo.setTagId(tagId);
                } else {
                    String tagIdMax = collect1.get(0).getTagId();
                    int i = Integer.parseInt(tagIdMax.substring(2, 4));
                    int i1 = i + 1;
                    String changeI;
                    if (i1 <= 9) {
                        changeI = "0" + i1;
                    } else {
                        changeI = String.valueOf(i1);
                    }
                    if (i >= 99) {
                        throw new RuntimeException("标签编号超过99不能添加啦");
                    } else {
                        String tagId = parentTagId.substring(0, 2) + changeI + tagIdMax.substring(4, 6);
                        tagInfo.setTagId(tagId);
                    }
                }
            } else if (tagInfo.getTagLevel().equals("3")) {
                if (collect1.isEmpty()) {
                    String tagId = parentTagId.substring(0, 4) + "01";
                    tagInfo.setTagId(tagId);
                } else {
                    String tagIdMax = collect1.get(0).getTagId();
                    int i = Integer.parseInt(tagIdMax.substring(4, 6));
                    int i1 = i + 1;
                    String changeI;
                    if (i1 <= 9) {
                        changeI = "0" + i1;
                    } else {
                        changeI = String.valueOf(i1);
                    }
                    if (i >= 99) {
                        throw new RuntimeException("标签编号超过99不能添加啦");
                    } else {
                        String tagId = tagIdMax.substring(0, 4) + changeI;
                        tagInfo.setTagId(tagId);
                    }
                }
            } else {
                throw new RuntimeException("你给的标签等级不对啊！！！");
            }
        }
        tagMapper.insertTagInfo(tagInfo);
    }

    @Transactional
    public void addImageTag(List<ImageTagBean> imageTagBeans) {
        for (ImageTagBean imageTagBean : imageTagBeans) {
            tagMapper.insertImageTag(imageTagBean);
        }
    }

    @Transactional
    public void deleteImageTag(List<ImageTagBean> imageTagBeans) {
        for (ImageTagBean imageTagBean : imageTagBeans) {
            tagMapper.deleteImageTag(imageTagBean);
        }
    }
}
